package com.biz.service.common;

import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.FileReader;

/**
 * Created by Louchen on 2016/7/6.
 */
@Service("mavenService")
public class MavenServiceImpl implements MavenServiceI {

    private String path;

    private Model model;

    @Autowired
    private ServletContext sc;

    @PostConstruct
    private void init(){
        path = sc.getRealPath(File.separator)+"META-INF"+File.separator+"maven"+File.separator+"com.iishang"+File.separator+"ec-operation"+File.separator+"pom.xml";
        MavenXpp3Reader reader = new MavenXpp3Reader();
        try {
            model = reader.read(new FileReader(path));
        } catch (Exception e) {
            //ignore
        }
    }

    /**
     * 获取pom.xml 中的version
     * @return String
     */
    @Override
    public String getVersion(){
        return model==null?"开发版":model.getVersion();
    }


}
